﻿using System;
using System.Text;
using Aspects.Custom;

namespace Aspects.Unit
{
    public class UnitModel
    {
        public static string GetModel()
        {
            var sb = new StringBuilder(8);
            var returnValue = "No Model Found"; //default value
            for (byte i = (byte)ECGetModel.GetModel7; i >= (byte)ECGetModel.GetModel0; i--)
            {
                byte ModelCharacter = ECPort.DoubleReadEC(i);
                if (checkByte(ModelCharacter))
                    sb.Append((char)ModelCharacter);
            }
            if (string.IsNullOrEmpty(sb.ToString()))
            {
                var temp = WmiProperty.GetProperty("Win32_ComputerSystem", System.Environment.MachineName, "Model");
                if (!string.IsNullOrEmpty(temp))
                {
                    returnValue = temp;
                }
            }
            else
            {
                returnValue = sb.ToString();
            }

            return returnValue;
        }

        public static DLIModel GetModelEnum()
        {
            var tempModel = GetModel();
            if (tempModel.Contains("8300M"))
                return DLIModel.DLI8300M;
            else if (tempModel.Contains("8300P"))
                return DLIModel.DLI8300P;
            else if (tempModel.Contains("8400"))
                return DLIModel.DLI8400;
            else if (tempModel.Contains("8500P"))
                return DLIModel.DLI8500P;
            else if (tempModel.Contains("8500"))
                return DLIModel.DLI8500;
            else if (tempModel.Contains("8800"))
                return DLIModel.DLI8800;
            else if (tempModel.Contains("9200"))
                return DLIModel.DLI9200;
            else
                return DLIModel.None;
        }

        public static void GetModelByte()
        {
            Console.WriteLine(ECPort.DoubleReadEC(0xFF));
        }

        /// <summary>
        /// Checks a byte to see if it is a number, lower letter or big letter.
        /// </summary>
        /// <param name="biting">byte to be checked</param>
        /// <returns>if it is a number or letter it returns it self otherwise it returns the number 0</returns>
        private static bool checkByte(byte biting)
        {
            if (biting >= 0x30 && biting <= 0x39)
            {
                return true;
            }
            else if (biting >= 0x41 && biting <= 0x5A)
            {
                return true;
            }
            else if (biting >= 0x61 && biting <= 0x7A)
            {
                return true;
            }
            else
                return false;
        }

        public enum DLIModel
        {
            DLI8300M,
            DLI8300P,
            DLI8400,
            DLI8400M,
            DLI8500,
            DLI8500P,
            DLI8800,
            DLI9200,
            DLI9212,
            DLI9215,
            None
        }

        private enum ECGetModel : byte
        {
            GetModel0 = 0xF0,
            GetModel1 = 0xF1,
            GetModel2 = 0xF2,
            GetModel3 = 0xF3,
            GetModel4 = 0xF4,
            GetModel5 = 0xF5,
            GetModel6 = 0xF6,
            GetModel7 = 0xF7
        }
    }
}
